#!/usr/bin/python3
# -*- coding: utf-8 -*-

from numpy import array,append,matrix
from sympy import Matrix as Matrix

def solve(Ab):
    reMatrix = Ab.rref()[0]
    pivot = Ab.rref()[1]

    m = Ab.shape[0]
    n = Ab.shape[1]
    i = 0
    if max(pivot) == n - 1:
        return None
    res = [[0 for _ in range(n - len(pivot))] for _ in range(n - 1)]
    while i <= m - 1:
        j = 0
        while j <= n - 2:
            if reMatrix[i, j] == 1 and j in pivot:
                pj = j
                k = 0
                res[j][k] = reMatrix[i, n-1]
                k += 1
            if reMatrix[i, j] != 0 and not j in pivot:
                res[pj][k] = -reMatrix[i, j]
            j = j + 1
        i = i + 1
    k = 1
    i = 0
    while i <= n - 2:
        if not i in pivot:
            res[i][k] = 1
            k += 1
        i += 1
    return matrix(res)

print(solve(Matrix([[1, 1, -3, -1, 1],
                    [3, -1, -3, 4, 4],
                    [1, 5, -9, -8, 0]])))